package com.github.tvbox.osc.ui.activity

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.viewpager2.widget.ViewPager2
import com.github.tvbox.osc.R
import com.github.tvbox.osc.base.BaseLazyFragment
import com.github.tvbox.osc.ui.activity.HomeAnim.Companion.startAnimHide
import com.github.tvbox.osc.ui.activity.HomeAnim.Companion.startAnimShow
import com.github.tvbox.osc.ui.activity.MediaCategoryManager.TabMediator
import com.github.tvbox.osc.viewmodel.SourceViewModel
import com.github.tvbox.osc.viewmodel.SourceViewModelKt
import io.github.source.media.bean.MediaCategory
import io.github.source.media.bean.MediaResult

/**
 * @ClassName: HomeFragment
 * @Description: 首页
 * @author: huangrongcai
 * @date: 2025/6/10
 *
 * @Copyright: 黄嵘才（HuangRc)
 */
class HomeActivityFragment : BaseLazyFragment() {

//    private var _binding: FragmentHomeBinding? = null
//    private val binding get() = _binding!!

    private val sourceViewModel by lazy {
        ViewModelProvider(this)[SourceViewModelKt::class.java]
    }

    private var mTopBarView: ViewGroup? = null
    private var mTvName: TextView? = null
    private var tvDate: TextView? = null
    private var toolbar: HomeToolbar? = null

    /** 分类标签管理 */
    private var categoryManager: MediaCategoryManager? = null

    //更新时间
    /**页面 ViewPager管理 */
    private var viewPagerManager: ViewPagerManager? = null

    companion object {
        /**创建对象*/
        @JvmStatic
        fun newInstance(): Fragment {
            return HomeActivityFragment()
        }
    }

    override fun getLayoutResID(): Int {
        return R.layout.fragment_home
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView(view)
        initViewModel()
    }

    fun initView(rootView: View?) {
        mTopBarView = rootView?.findViewById(R.id.skin_home_TopLayout)
        mTvName = rootView?.findViewById(R.id.skin_home_TvName)
        tvDate = rootView?.findViewById(R.id.skin_home_TvDate)

        val categoryView: View? = rootView?.findViewById(R.id.skin_home_GridView)
        val viewPager: ViewPager2? = rootView?.findViewById(R.id.skin_home_ViewPager)

        categoryManager = MediaCategoryManager.create(this, categoryView)
        viewPagerManager = ViewPagerManager.create(this, viewPager)

        TabMediator(
            categoryManager,
            viewPagerManager
        ) { item: MediaCategory?, position: Int ->
            showTopBar(position == 0)

        }.attach()

    }

    private fun initViewModel() {
        sourceViewModel.categoryResult?.observe(
            this.viewLifecycleOwner
        ) { result: MediaResult<List<MediaCategory?>?>? ->
            hideLoading()
            showSuccess()
            //分类TAB（+TAB内容）
            initCategoryView(result)
            //分类页面（+页面内容）
            initViewPager(result)

        }
    }

    override fun initData() {
        //检查页面空，则先显示loading
        if (isLoading()) {
            showLoading()
        }
        sourceViewModel.fetchCategory()
    }

    private fun initCategoryView(list: MediaResult<List<MediaCategory?>?>?) {
        if (list != null) {
            //分类数据(TAB)
            categoryManager?.setNewData(list.data)
        } else {
            categoryManager?.setNewData(null)
        }

    }

    private fun initViewPager(list: MediaResult<List<MediaCategory?>?>?) {
        if (list != null) {
            //分类数据(TAB)
            viewPagerManager?.setNewData(list.data)
        } else {
            viewPagerManager?.setNewData(null)
        }
    }

    private fun isLoading(): Boolean {
        return false
    }

    private fun showTopBar(show: Boolean) {
        if (show) {
            mTopBarView?.let {
                it.post {
                    startAnimShow(it)
                }
            }
        } else {
            mTopBarView?.let {
                it.post {
                    startAnimHide(it)
                }
            }
        }
    }

}